package design.pattern.Lu17FacadePattern;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @program JavaBase
 * @description: 门面模式
 * @author: zhanglu
 * @create: 2020-04-10 15:18:00
 */
public class FacadePattern {

//    public static void main(String[] args) {
//        //1月 1个亿，1天300w 多货主库存日志刷数据202506
//        String sql = "-- 2025-06-20\n" +
//                "UPDATE `item_stock_log` a JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` \n" +
//                "set a.`cargo_owner_id` = b.`cargo_owner_id` \n" +
//                "where a.company_id=66666 and a.`create_time`>='2025-06-20' \n" +
//                "and a.`create_time` < '2025-06-21' and a.cargo_owner_id is null; ";
//        sql = "" +
//                "UPDATE `item_stock_log` a JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` " +
//                "set a.`cargo_owner_id` = b.`cargo_owner_id` " +
//                "where a.company_id=66666 and a.`create_time`>='{}' " +
//                "and a.`create_time`<'{}' and a.store_id={} and a.cargo_owner_id is null; ";
////        sql = "" +
////                "UPDATE `stock_detail` a JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` " +
////                "set a.`cargo_owner_id` = b.`cargo_owner_id` " +
////                "where a.company_id=66666 and a.`create_time`>='{}' " +
////                "and a.`create_time`<'{}' and a.cargo_owner_id is null;";
//        List<String> storeIds = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/lu/workspace/众丞/storeIds.txt").stream().distinct().collect(Collectors.toList());
////        List<String> itemIds = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/lu/workspace/众丞/itemIds.txt").stream().distinct().collect(Collectors.toList());
//        DateTime prev = DateUtil.parseDate("2025-01-01");
//        DateTime last = DateUtil.parseDate("2025-06-16");
//        String finalSql = sql;
////        List<List<String>> lists = Lists.partition(itemIds, 100);
//        List<String> sqls = new ArrayList<>();
//        DateUtil.rangeToList(prev, last, DateField.DAY_OF_YEAR, 1).stream().sorted(Comparator.reverseOrder()).forEach(o -> {
//            String start = DateUtil.formatDate(o);
//            String end = DateUtil.formatDate(DateUtil.offsetDay(o, 1));
//            for (String storeId : storeIds) {
////                for (List<String> list : lists) {
//                   sqls.add(CharSequenceUtil.format(finalSql, start, end, storeId));
//                    System.out.println(CharSequenceUtil.format(finalSql, start, end, storeId));
////                }
//            }
//        });
//        final List<List<String>> partition = Lists.partition(sqls, 1000);
//        for (int i = 0; i < partition.size(); i++) {
//            File file = FileUtil.newFile("/Users/zhanglu/Desktop/lu/workspace/众丞/tmp/"+i+".txt");
//            FileUtil.appendUtf8Lines(partition.get(i), file);
//        }
//    }

    public static void main(String[] args) {
        //1月 1个亿，1天300w 多货主库存日志刷数据202506
        String sql = "\n" +
                "UPDATE \n" +
                "`pick_order` a \n" +
                "LEFT JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` \n" +
                "SET a.`cargo_owner_id` = b.`cargo_owner_id`\n" +
                "WHERE a.company_id=66666 and a.create_time >= '{}' and a.create_time<'{}' and a.`store_id` = {} and a.cargo_owner_id is null;";
//        sql = "" +
//                "UPDATE `stock_detail` a JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` " +
//                "set a.`cargo_owner_id` = b.`cargo_owner_id` " +
//                "where a.company_id=66666 and a.`create_time`>='{}' " +
//                "and a.`create_time`<'{}' and a.cargo_owner_id is null;";
        List<String> storeIds = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/lu/workspace/众丞/storeIds.txt").stream().distinct().collect(Collectors.toList());
//        List<String> itemIds = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/lu/workspace/众丞/itemIds.txt").stream().distinct().collect(Collectors.toList());
        DateTime prev = DateUtil.parseDate("2025-06-01");
        DateTime last = DateUtil.parseDate("2025-07-01");
        String finalSql = sql;
//        List<List<String>> lists = Lists.partition(itemIds, 100);
        List<String> sqls = new ArrayList<>();
        DateUtil.rangeToList(prev, last, DateField.DAY_OF_YEAR, 1).stream().sorted(Comparator.reverseOrder()).forEach(o -> {
            String start = DateUtil.formatDate(o);
            String end = DateUtil.formatDate(DateUtil.offsetDay(o, 1));
            for (String storeId : storeIds) {
//                for (List<String> list : lists) {
                sqls.add(CharSequenceUtil.format(finalSql, start, end, storeId));
                System.out.println(CharSequenceUtil.format(finalSql, start, end, storeId));
//                }
            }
        });
        final List<List<String>> partition = Lists.partition(sqls, 1000);
        for (int i = 0; i < partition.size(); i++) {
            File file = FileUtil.newFile("/Users/zhanglu/Desktop/lu/workspace/众丞/tmp2/"+i+"xxx.txt");
            FileUtil.appendUtf8Lines(partition.get(i), file);
        }
    }

    public static void main2(String[] args) {
        //1月 1个亿，1天300w
        final List<String> strings = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/lu/workspace/众丞/1.txt");
        String sql = "" +
                "UPDATE `stock_detail` a JOIN `cargo_owner_conf_relation` b on a.`store_id` = b.`store_id` set a.`cargo_owner_id` = b.`cargo_owner_id` " +
                "where a.company_id=66666 and a.cargo_owner_id is null and a.create_time>='2025-06-16' and a.item_id in ({});";
        sql = "update stock_detail set `cargo_owner_id` =`store_id`\n" +
                "where company_id != 1001 and company_id != 66666 and `cargo_owner_id` is null and item_id in ({});";
        //        Arrays.stream(s.split("\n")).forEach(o -> {
//            DateTime dateTime = DateUtil.parseDate(o);
//            DateTime start = DateUtil.beginOfMonth(dateTime);
//            DateTime end = DateUtil.beginOfMonth(DateUtil.offsetMonth(dateTime, 1));
//            System.out.println(CharSequenceUtil.format(sql, start, end));
//        });
        List<List<String>> lists = Lists.partition(strings, 10);
        for (List<String> list : lists) {
            System.out.println(CharSequenceUtil.format(sql, StrUtil.join(",", list)));
        }
    }

//    public static void main(String[] args) {
////        Facade facade = new Facade();
////        facade.todo01();
////        facade.todo02();
////        facade.todo03();
//
//        System.out.println(new BigDecimal("0.0002").multiply(new BigDecimal("0.003")));
//
//
//    }

}

interface Learning{
    void method();
}

class Student01 implements Learning{

    @Override
    public void method() {
        System.out.println("看书");
    }
}

class Student02 implements Learning{

    @Override
    public void method() {
        System.out.println("看视频");
    }
}

class Student03 implements Learning{

    @Override
    public void method() {
        System.out.println("冥想");
    }
}

class Facade{

    private Learning l1 = new Student01();
    private Learning l2 = new Student02();
    private Combination01 l3 = new Combination01();

    public void todo01(){
        l1.method();
    }

    public void todo02(){
        l2.method();
    }

    public void todo03(){
        l3.method();
    }

}

class Combination01{

    private Learning l1 = new Student01();
    private Learning l3 = new Student03();

    public void method(){
        l1.method();
        l3.method();
    }
}

